﻿using System;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using DXTools.UI.Core;
using DXTools.UI.Modules.ODataQueryDesigner.Views;
using DXTools.UI.Core.Events;
using DXTools.UI.Core.ViewModel;

namespace DXTools.UI.Modules.ODataQueryDesigner.ViewModels
{
    public class ToolbarViewModel : ViewModelBase
    {

        public DelegateCommand<object> ViewDesignerCommand { get; private set; }
        public DelegateCommand<object> ExecuteCommand { get; private set; }

        public ToolbarViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer container)
            : base(regionManager, eventAggregator, container)
        {
            this.HeaderInfo = "OData Query Designer";
            this.ViewDesignerCommand = new DelegateCommand<object>(this.ViewDesigner, this.CanViewDesigner);
            //this.ExecuteCommand = new DelegateCommand<object>(this.Execute, this.CanExecute);

            // View collection changed
            //this.RegionManager.Regions["BodyRegion"].ActiveViews.CollectionChanged += (sender, e) =>
            //{
            //    ExecuteCommand.RaiseCanExecuteChanged();
            //};

        }

        private void ViewDesigner(object commandArg)
        {
            // Single View
            string viewName = typeof(DesignerView).FullName;
            IRegion bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            object view = bodyRegion.GetView(viewName);
            if (view == null)
            {
                DesignerView newView = UnityContainer.Resolve<DesignerView>();
                bodyRegion.Add(newView, viewName);
                bodyRegion.Activate(newView);
                EventAggregator.GetEvent<ViewAddedEvent>().Publish(newView);
            }
            else
                bodyRegion.Activate(view);

        }

        private bool CanViewDesigner(object commandArg)
        {
            return true;
        }

        //private bool CanExecute(object commandArg)
        //{
        //    return true;
        //}

        //private void Execute(object commandArg)
        //{



        //}




    }
}
